package test;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.donhuan.rms.ui.importWizards.MyWizard;
import org.donhuan.rms.ui.importWizards.NewProjectCreationWizard;
import org.donhuan.rms.ui.model.Project;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Table;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.layout.TableColumnLayout;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.databinding.viewers.ObservableListContentProvider;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.donghuan.rms.framework.domain.Archive;
import org.donghuan.rms.framework.domain.Person;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProvider;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.list.WritableList;
import org.eclipse.core.databinding.beans.PojoProperties;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.ColumnPixelData;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.donhuan.rms.ui.controls.CompositeTextToolbar;
import org.eclipse.wb.swt.ResourceManager;
import org.eclipse.swt.widgets.ToolBar;
import org.donghuan.rms.framework.domain.RecordFile;
import org.donghuan.rms.framework.domain.RecordPage;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.core.databinding.beans.BeansObservables;

public class MainWindow {
	private DataBindingContext m_bindingContext;

	protected Shell shell;
	private Project project;
	private ViewForm viewForm;
	private CompositeTextToolbar compositeTextToolbar;
	private ToolBar toolBar;
	private Table table;
	private TableViewer tableViewer;
	/**
	 * @wbp.nonvisual location=35,337
	 */
	private final RecordFile recordFile = new RecordFile();
	private IObservableList input;
	private ToolItem tltmNewItem;
	private ToolItem tltmNewItem_1;
	private TableColumn tblclmnNewColumn;
	private TableColumn tblclmnNewColumn_1;
	private TableColumn tblclmnNewColumn_2;
	private TableColumn tblclmnNewColumn_3;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		Display display = Display.getDefault();
		Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
			public void run() {
				try {
					MainWindow window = new MainWindow();
					window.open();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(450, 300);
		shell.setText("SWT Application");
//		MyWizard wizard=new MyWizard();
//		WizardDialog d=new WizardDialog(shell,wizard);
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		viewForm = new ViewForm(shell, SWT.NONE);
		
		compositeTextToolbar = new CompositeTextToolbar(viewForm, SWT.NONE);
		viewForm.setTopLeft(compositeTextToolbar);
		
		toolBar = new ToolBar(viewForm, SWT.FLAT | SWT.RIGHT);
		viewForm.setTopRight(toolBar);
		
		tltmNewItem_1 = new ToolItem(toolBar, SWT.NONE);
		tltmNewItem_1.setText("New Item");
		
		
		tableViewer = new TableViewer(viewForm, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		viewForm.setContent(table);
		
		tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(100);
		tblclmnNewColumn.setText("\u540D\u79F0");
		
		tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(100);
		tblclmnNewColumn_1.setText("New Column");
		
		tblclmnNewColumn_2 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_2.setWidth(100);
		tblclmnNewColumn_2.setText("New Column");
		
		tblclmnNewColumn_3 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_3.setWidth(100);
		tblclmnNewColumn_3.setText("New Column");
		Action actions=new Action() {
			@Override
			public void run() {
				if(recordFile.getRecordPages()==null){
					recordFile.setRecordPages(new ArrayList<RecordPage>());
				}
				input.add(new RecordPage("a"));
				tableViewer.refresh();
			}
		};
		actions.setImageDescriptor(ResourceManager.getPluginImageDescriptor("org.donhuan.rms.ui", "icons/gif16/add.gif"));
		compositeTextToolbar.addActionItem(actions);
		m_bindingContext = initDataBindings();

	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}
	protected DataBindingContext initDataBindings() {
		DataBindingContext bindingContext = new DataBindingContext();
		//
		ObservableListContentProvider listContentProvider = new ObservableListContentProvider();
		IObservableMap observeMap = BeansObservables.observeMap(listContentProvider.getKnownElements(), RecordPage.class, "recordPageName");
		tableViewer.setLabelProvider(new ObservableMapLabelProvider(observeMap));
		tableViewer.setContentProvider(listContentProvider);
		//
//		IObservableList recordPagesRecordFileObserveList = PojoProperties.list("recordPages").observe(recordFile);
		input = PojoProperties.list("recordPages").observe(recordFile);
		tableViewer.setInput(input);
		//
		return bindingContext;
	}
}
